<!-- 选择供货清单弹窗 -->
<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="选择供货清单"
    width="70%"
    draggable
    overflow
    @close="handleClose"
  >
    <div class="crossRope" />
    <el-form style="margin-top: 2%">
      <el-row>
        <el-col :span="6">
          <el-form-item label="订单编号" style="width: 95%">
            <el-input v-model="searchForm.orderSn" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="订单名称" style="width: 95%">
            <el-input v-model="searchForm.orderName" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="合同名称" style="width: 95%">
            <el-input
              v-model="searchForm.contractName"
              size="small"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称" style="width: 95%">
            <el-input v-model="searchForm.projectName" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="text-align: right">
          <el-button
            size="small"
            color="#1E3485"
            :icon="Search"
            @click="
              tablePage.currentPage = 1;
              getList();
            "
            >搜索</el-button
          >
          <el-button size="small" :icon="Delete" @click="resetSearchForm()"
            >清空</el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <vxe-table
      ref="tableRef"
      border
      min-height="350"
      max-height="550"
      header-align="center"
      :column-config="{ resizable: true }"
      :row-config="{ isHover: true }"
      :data="tableData"
      size="mini"
      style="margin-top: 2%"
      :loading="tableLoading"
      :pager-config="tablePage"
    >
      <vxe-column type="checkbox" width="60" align="center" fixed="left" />
      <vxe-column
        v-for="column in tableColumns"
        :key="column.field"
        :field="column.field"
        :title="column.title"
        :fixed="column.fixed"
        :align="column.align"
        :min-width="column.width"
        show-overflow
      >
        <template v-slot:default="{ row }">
          {{ row[column.field] }}
        </template>
      </vxe-column>
      <vxe-column title="操作" min-width="100" align="center" fixed="right">
        <template v-slot:default="{ row }">
          <vxe-button
            type="text"
            size="small"
            @click="
              toDetail(
                { id: row.id, title: row.orderName, action: 'view' },
                'viewSupplyList'
              )
            "
            >查看</vxe-button
          >
        </template>
      </vxe-column>
    </vxe-table>
    <vxe-pager
      v-model:current-page="tablePage.currentPage"
      v-model:page-size="tablePage.pageSize"
      :layouts="[
        'Sizes',
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'FullJump',
        'Total'
      ]"
      :total="tablePage.total"
      size="mini"
      style="margin-top: 2%"
      @page-change="handlePageChange"
    />
    <div class="crossRope" />
    <div style="text-align: right; margin-top: 2%">
      <el-button
        color="#1E3485"
        :icon="Select"
        size="small"
        @click="selectConfirm"
        >确定</el-button
      >
      <el-button :icon="CloseBold" size="small" @click="handleClose"
        >关闭</el-button
      >
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { ElDialog, ElMessage } from "element-plus";
import { Delete, Search, Select, CloseBold } from "@element-plus/icons-vue";
import { useDetail } from "@/views/ERP_SupplyManage/ERP_SupplyList/components/utils/hooks";
import { VxePagerEvents, VxeTable } from "vxe-table";
import { getsosupplylist } from "@/api/ERP_SupplyManage";

// 路由跳转
const { toDetail, router } = useDetail();
// 弹窗关闭/打开
const props = defineProps({
  visible: Boolean,
  contractId: String,
  idList: Object
});
const dialogFormVisible = ref(false);
const ids = ref([]);
const emit = defineEmits(["close", "confirm"]);
function handleClose() {
  emit("close", false);
}
watch(
  () => props.visible,
  val => {
    dialogFormVisible.value = val;
    if (val === true) {
      if (props.idList.length > 0) {
        ids.value = [];
        for (let i in props.idList) {
          ids.value.push(props.idList[i].id);
        }
      } else {
        ids.value = null;
      }
    }
  },
  { immediate: true }
);

// 搜索表单
let searchForm = reactive({
  orderSn: "", // 订单编号
  orderName: "", // 订单名称
  projectName: "", // 项目名称
  contractName: "" // 合同名称
});

// 表格配置
let tableLoading = ref(false);
let tableData = ref([]);
let tableRef = ref(null);
const tableColumns = reactive([
  {
    field: "orderSn",
    title: "订单编号",
    width: 150,
    fixed: "left",
    align: "left"
  },
  {
    field: "orderName",
    title: "订单名称",
    width: 150,
    fixed: "left",
    align: "left"
  },
  {
    field: "contractName",
    title: "合同名称",
    width: 150,
    align: "left"
  },
  {
    field: "projectName",
    title: "项目名称",
    width: 150,
    align: "left"
  },
  {
    field: "taxTotalPrice",
    title: "供货含税总金额（元）",
    width: 150,
    align: "right"
  },
  {
    field: "shipmentAt",
    title: "供货时间",
    width: 150,
    align: "center"
  },
  {
    field: "takeAt",
    title: "验收时间",
    width: 150,
    align: "center"
  }
]);
// 表格分页配置
const tablePage = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 10
});
// 表格分页事件
const handlePageChange: VxePagerEvents.PageChange = ({
  currentPage,
  pageSize
}) => {
  tablePage.currentPage = currentPage;
  tablePage.pageSize = pageSize;
  getList();
};
// 清空按钮
function resetSearchForm() {
  searchForm.orderSn = "";
  searchForm.orderName = "";
  searchForm.contractName = "";
  searchForm.projectName = "";
}
// 获取表格数据
async function getList() {
  tableLoading.value = true;
  let params: ApiParams = {};
  params.pageIndex = tablePage.currentPage;
  params.pageSize = tablePage.pageSize;
  params.contractId = props.contractId;
  params.reconciliationStatus = 0;
  params.supplyStatusList = 5;
  params.isEliminateNotInvoice = "";
  if (searchForm.orderSn) params.orderSn = searchForm.orderSn;
  if (searchForm.orderName) params.orderName = searchForm.orderName;
  if (searchForm.projectName) params.projectName = searchForm.projectName;
  if (searchForm.contractName) params.contractName = searchForm.contractName;
  if (ids.value != null) {
    params.idList = ids.value;
  }
  try {
    const response: ApiResponse = await getsosupplylist(params);
    tableData.value = response.data.records;
    tablePage.total = response.data.totalElements;
  } catch (error) {
    ElMessage.error("获取表格数据失败");
  } finally {
    tableLoading.value = false;
  }
}
// 获取选中行
function getSelectedRows() {
  return tableRef.value.getCheckboxRecords();
}
// 确认事件
function selectConfirm() {
  if (getSelectedRows().length === 0) {
    ElMessage.error("请选择一条数据");
  } else {
    emit("confirm", {
      selectedRow: getSelectedRows()
    });
    // 关闭弹窗
    handleClose();
  }
}

// 打开弹窗时执行
watch(dialogFormVisible, newValue => {
  if (newValue) {
    getList();
  }
});
</script>
